package com;


import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
/*

Redis 操作函数，名为 T1insert。它通过 hiredis 库连接到 Redis 数据库，执行一系列的 SET 和 GET 操作，并测量这些操作的时间
 */

@SpringBootTest
public class T1insert {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private static final int HASH_LEN = 32;
    private static final int COUNTER = 10;

    @Test
    void contextLoads() {
    }

    @Test
    public void testRedisOperations() {
        long tstart, trend;
        double timer = 0;

        byte[] key1 = new byte[]{
                (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE, (byte) 0xFF, (byte) 0xFF, (byte) 0x11,
                (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
                (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
                (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
        };

        byte[] key = new byte[]{
                (byte) 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE, (byte) 0xFF, (byte) 0xFF, (byte) 0x11,
                (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
                (byte) 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFE, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
                (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
        };

        byte[] value = new byte[]{
                (byte) 0x01, (byte) 0x11, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xFF,
                (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
                (byte) 0x01, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xEE, (byte) 0xFF, (byte) 0xFF,
                (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xEE
        };

        byte[] EK = new byte[2 * key.length];
        byte[] EV = new byte[2 * value.length];
        byte[] DV = new byte[value.length];

        // Insert operation
        for (int i = 0; i < COUNTER; i++) {
            value[0] = (byte) i;
            key[0] = (byte) i;

            Encode(EK, key, key.length);
            Encode(EV, value, value.length);

            String encodedKey = char2str(EK, EK.length);
            String encodedValue = char2str(EV, EV.length);

            tstart = System.nanoTime();
            stringRedisTemplate.opsForValue().set(encodedKey, encodedValue);
            trend = System.nanoTime();

            timer += (trend - tstart) / 1000.0;
        }

        System.out.println("get:");
        // Get operation
        for (int i = 0; i < COUNTER; i++) {
            key[0] = (byte) i;
            Encode(EK, key1, key1.length);
            String encodedKey = char2str(EK, EK.length);

            tstart = System.nanoTime();
            String reply = stringRedisTemplate.opsForValue().get(encodedKey);
            trend = System.nanoTime();

            if (reply == null) {
                System.out.println("get from redis failed ...");
            } else {
                timer += (trend - tstart) / 1000.0;
                Decode(DV, reply.getBytes(), reply.length());
                strPrint(DV, DV.length);
            }
        }

        System.out.println("Total time: " + timer + " microseconds");
    }

    // Placeholder for Encode method
    public void Encode(byte[] dest, byte[] src, int len) {

        // Your encoding logic here
    }

    // Placeholder for char2str method
    public static String char2str(byte[] array, int length) {
        // 只取前 length 个字节转换为字符串
        return new String(array, 0, length, StandardCharsets.UTF_8);
    }

    // Placeholder for Decode method
    public void Decode(byte[] dest, byte[] src, int len) {
        // Your decoding logic here
    }

    // Placeholder for strPrint method
    public static void strPrint(byte[] data, int length) {
        for (int i = 0; i < length; i++) {
            System.out.printf("%02x ", data[i]);
        }
        System.out.println();
    }

}
